package com.pet.server.ext.action.product;

import java.util.List;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.Image;
import com.pet.server.ext.data.ProductInfo;
import com.pet.server.ext.service.ProductService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonUtil;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

public class Detail extends ActionBase{

	private Integer product_id;
	public Detail(){
		 needCheckAuth =false;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {	
		product_id=NumberUtil.getInteger(params.getFieldValue("product_id"));
		if(StringUtil.isEmpty(product_id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		ProductService productService = new ProductService();
		UserService userService = new UserService();
		ProductInfo p = productService.findProductInfoByProductId(product_id);
		if(null==p){
			throw ApiError.Type.LOGIC_ERROR.toException("can not find product error!");
		}
		
		
		List<Image> list = userService.queryImage(product_id, BaseAttribute.image_type.product_info.toString());
		Result result = new Result();
		result.put("data",JsonUtil.productDetail(list, p,"product_detail"));
		return result;
	}
}
